var app = getApp();
Page({
    data: {
        array: [
            "京",
            "浙",
            "津",
            "皖",
            "沪",
            "闽",
            "渝",
            "赣",
            "港",
            "鲁",
            "澳",
            "豫",
            "蒙",
            "鄂",
            "新",
            "湘",
            "宁",
            "粤",
            "藏",
            "琼",
            "桂",
            "川",
            "蜀",
            "冀",
            "贵",
            "黔",
            "晋",
            "云",
            "滇",
            "辽",
            "陕",
            "吉",
            "甘",
            "陇",
            "黑",
            "青",
            "苏",
            "台"
        ],
        index: 2, //省份简写数组索引值
        proviceName: "津",
        items: [{
                name: '1',
                value: '个人',
                checked: 'true'
            },
            {
                name: '2',
                value: '企业'
            }
        ]
    },
    radioChange: function (e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    // onLoad: function() {
    //     if (wx.getStorageSync('phone') == 0) {
    //         wx.showToast({
    //             title: '请先绑定手机号码',
    //             icon: 'none',
    //             success: function() {
    //                 setTimeout(function() {
    //                     wx.navigateTo({
    //                         url: '/pages/changePhone/changePhone',
    //                     })
    //                 }, 1000)
    //             }
    //         })
    //     }
    //     var date = new Date();
    //     var endTime = app.utils.timeToDate(date);
    //     this.setData({
    //         endTime: endTime,
    //         registerDate: endTime,
    //     });
    //     //移除车辆三级联动的缓存
    //     // wx.removeStorageSync("b_id");
    //     // wx.removeStorageSync("pinpaiName");
    //     // wx.removeStorageSync("t_id");
    //     // wx.removeStorageSync("chexiName");
    //     // wx.removeStorageSync("m_id");
    //     // wx.removeStorageSync("chexingName");
    //     var u_id = wx.getStorageSync("u_id");
    //     this.setData({
    //         u_id: u_id
    //     });
    // },
    /**
     * 生命周期函数--监听页面显示
     */
    // onShow: function() {
    //     // 判断是否登录
    //     app.getUserInfoFun();
    //     this.setData({
    //         b_id: wx.getStorageSync("b_id"),
    //         t_id: wx.getStorageSync("t_id"),
    //         m_id: wx.getStorageSync("m_id"),
    //         pinpaiName: wx.getStorageSync("pinpaiName"),
    //         chexiName: wx.getStorageSync("chexiName"),
    //         chexingName: wx.getStorageSync("chexingName"),
    //         CarNum: wx.getStorageSync("CarNum")
    //     });
    // },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        //移除车辆三级联动的缓存
        wx.removeStorageSync("b_id");
        wx.removeStorageSync("pinpaiName");
        wx.removeStorageSync("t_id");
        wx.removeStorageSync("chexiName");
        wx.removeStorageSync("m_id");
        wx.removeStorageSync("chexingName");
    },

    bindDevice: function (e) {
        this.setData({
            device_name: e.detail.value
        });
    },
    bindPickerChange: function (e) {
        this.setData({
            index: e.detail.value,
            proviceName: this.data.array[e.detail.value]
        });
    },
    //提交按钮点击事件
    showTopTips: function () {
        var carnum = this.data.CarNum;
        var UPCarNum = carnum.toLocaleUpperCase();
        // 车牌号码正则
        var express = /^[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
        var that = this;
        if (!express.test(UPCarNum)) {
            this.setData({
                warnMsg: "车牌号码输入有误"
            });
            this.setData({
                showTopTips: true
            });
            setTimeout(function () {
                that.setData({
                    showTopTips: false
                });
            }, 2000);
        } else {
            //ajax提交
            var that = this;
            var u_id = wx.getStorageSync("u_id");
            var logo = wx.getStorageSync("logo");
            var url = "api/car/addCar";
            var data = {
                u_id: u_id,
                b_id: this.data.b_id,
                t_id: this.data.t_id,
                m_id: this.data.m_id,
                b_name: this.data.pinpaiName,
                t_name: this.data.chexiName,
                m_name: this.data.chexingName,
                licenseplate: this.data.proviceName + UPCarNum,
                device_name: this.data.device_name,
                logo: logo,
                buycartime: this.data.registerDate, //购买时间
                licenseplate_type: this.data.isCheck ? 2 : 0
            };
            var callback = res => {
                if (res.code == 200) {
                    //移除车辆三级联动的缓存
                    wx.removeStorageSync("b_id");
                    wx.removeStorageSync("pinpaiName");
                    wx.removeStorageSync("t_id");
                    wx.removeStorageSync("chexiName");
                    wx.removeStorageSync("m_id");
                    wx.removeStorageSync("chexingName");
                    wx.setStorageSync("cartype_id", that.pinpaiId);
                    that.setData({
                        warnMsg: "添加成功"
                    });
                    that.setData({
                        showTopTips: true
                    });
                    setTimeout(function () {
                        that.setData({
                            showTopTips: false
                        });
                    }, 2000);
                    setTimeout(function () {
                        wx.switchTab({
                            url: "../tabBar/mine/mine"
                        });
                    }, 2000);
                } else if (res.code == 201) {
                    that.setData({
                        warnMsg: res.message
                    });
                    that.setData({
                        showTopTips: true
                    });
                    setTimeout(function () {
                        that.setData({
                            showTopTips: false
                        });
                    }, 2000);
                }
            };
            app.getRequest(url, data, callback);
        }
    },
    bindCarInput: function (e) {
        this.setData({
            CarNum: (e.detail.value).toUpperCase().trim(),
        });
        wx.setStorageSync("CarNum", this.data.CarNum);
    },
    //购买日期时间选择
    bindTimeChange: function (e) {
        this.setData({
            registerDate: e.detail.value,
        })
    },
    //点击跳转到三级联动页面
    tolinkage: function () {
        wx.removeStorageSync('b_id');
        wx.removeStorageSync('t_id');
        wx.removeStorageSync('m_id');
        wx.removeStorageSync('pinpaiName');
        wx.removeStorageSync('chexiName');
        wx.removeStorageSync('chexingName');
        wx.navigateTo({
            url: "../linkage/linkage"
        });
    },
    //品牌的ajax
    // bindPinpaiChange: function (e) {
    //   this.data.pinpaiName = this.data.pinpai[e.detail.value];
    //   this.setData({
    //     pinpaiIndex: e.detail.value,
    //     pinpaiId: this.data.pinpaiIds[e.detail.value],
    //     logo: this.data.logos[e.detail.value]
    //   });
    //   wx.setStorageSync(
    //     "defaultCar_img",
    //     app.globalData.globalUrl + this.data.logo
    //   );
    //   var thisID = this.data.pinpaiIds[e.detail.value];
    //   thisID = thisID - 0;
    //   var that = this;
    //   var url = "obd/car/selectCarop";
    //   var data = {
    //     p_id: thisID
    //   };
    //   var callback = res => {
    //     var chexi = [];
    //     var chexis = res.data;
    //     var chexiIds = [];
    //     for (var i = 0; i < chexis.length; i++) {
    //       chexi.push(chexis[i].NAME);
    //       chexiIds.push(chexis[i].ID);
    //     }
    //     that.setData({
    //       chexi: chexi,
    //       chexiIds: chexiIds
    //     });
    //   };
    //   app.getRequest(url, data, callback);
    // },

    //车系的ajax
    // bindChexiChange: function (e) {
    //   this.data.chexiName = this.data.chexi[e.detail.value];
    //   this.setData({
    //     chexiIndex: e.detail.value,
    //     chexiId: this.data.chexiIds[e.detail.value]
    //   });
    //   var thisID = this.data.chexiIds[e.detail.value];
    //   thisID = thisID - 0;
    //   var that = this;
    //   var url = "obd/car/selectCarop";
    //   var data = {
    //     p_id: thisID
    //   };
    //   var callback = res => {
    //     var chexing = [];
    //     var chexings = res.data;
    //     var chexingIds = [];
    //     for (var i = 0; i < chexings.length; i++) {
    //       chexing.push(chexings[i].NAME);
    //       chexingIds.push(chexings[i].ID);
    //     }
    //     that.setData({
    //       chexing: chexing,
    //       chexingIds: chexingIds
    //     });
    //   };
    //   app.getRequest(url, data, callback);
    // },

    //车型的ajax
    // bindChexingChange: function (e) {
    //   this.setData({
    //     chexingIndex: e.detail.value,
    //     chexingId: this.data.chexingIds[e.detail.value],
    //     chexingName: this.data.chexing[e.detail.value]
    //   });
    // },
    //扫码的ajax
    sm: function () {
        //扫码
        wx.scanCode({
            onlyFromCamera: true,
            success: res => {
                // success
                this.setData({
                    device_name: res.result
                });
                wx.showToast({
                    title: "成功",
                    icon: "seccess",
                    duration: 1000
                });
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        });
    },
    isCheck: function () {
        this.setData({
            isCheck: !this.data.isCheck,
        })
    }

});